package com.example.tioClient;

import com.example.common.ClientConst;
import com.example.tioClient.handler.ClientHandlerBase;
import com.example.tioClient.handler.TioClientAioHandler;
import com.example.tioClient.listener.ClientListenerBase;
import com.example.tioClient.listener.TioClientAioListener;
import org.tio.client.ClientChannelContext;
import org.tio.client.ClientTioConfig;
import org.tio.client.ReconnConf;
import org.tio.client.TioClient;
import org.tio.core.Node;
import org.tio.core.Tio;

import java.io.IOException;

public class ClientStarter {


    //服务器节点
    private  Node serverNode ;
    //断链后自动连接的，不想自动连接请设为null
    private  ReconnConf reconnConf=null ;
    //一组连接共用的上下文对象
    private  ClientTioConfig clientTioConfig;
    private  TioClient tioClient;
    public static  ClientChannelContext clientChannelContext;
    private String serverIP= ClientConst.SERVER;
    private int port=ClientConst.PORT;

    private Integer connTime=5;

    private boolean isReconnConf=false;



    public ClientStarter(){
        serverNode = new Node(serverIP, port);
    }

    public ClientStarter(String server,int port){
        this.serverIP=server;
        this.port=port;
        serverNode = new Node(server, port);
    }


    public void setClientListener(ClientHandlerBase handler,boolean isReconnConf){
        if(isReconnConf){
            reconnConf = new ReconnConf(ClientConst.TIMEOUT);
            this.isReconnConf=isReconnConf;
        }
        clientTioConfig = new ClientTioConfig(new TioClientAioHandler(handler), null, reconnConf);
        clientTioConfig.setHeartbeatTimeout(ClientConst.TIMEOUT);
    }
    public void setClientListener(ClientHandlerBase handler,long heartbeatTimeout,boolean isReconnConf){
        if(isReconnConf){
            reconnConf = new ReconnConf(ClientConst.TIMEOUT);
            this.isReconnConf=isReconnConf;
        }
        clientTioConfig = new ClientTioConfig(new TioClientAioHandler(handler), null, reconnConf);
        clientTioConfig.setHeartbeatTimeout(heartbeatTimeout);
    }
    public void setClientListener(ClientHandlerBase handler, ClientListenerBase listenerBase,boolean isReconnConf){
        if(isReconnConf){
            reconnConf = new ReconnConf(ClientConst.TIMEOUT);
            this.isReconnConf=isReconnConf;
        }
        clientTioConfig = new ClientTioConfig(new TioClientAioHandler(handler), new TioClientAioListener(listenerBase), reconnConf);
        clientTioConfig.setHeartbeatTimeout(ClientConst.TIMEOUT);
    }
    public void setClientListener(ClientHandlerBase handler,ClientListenerBase listenerBase,long heartbeatTimeout,boolean isReconnConf){
        if(isReconnConf){
            reconnConf = new ReconnConf(ClientConst.TIMEOUT);
            this.isReconnConf=isReconnConf;
        }
        clientTioConfig = new ClientTioConfig(new TioClientAioHandler(handler), new TioClientAioListener(listenerBase), reconnConf);
        clientTioConfig.setHeartbeatTimeout(heartbeatTimeout);
    }

    /**
     * 连接检查时间默认5秒（秒）
     * @param connTime
     */
    public void setConnTime(Integer connTime){
        if(connTime!=null&&connTime.intValue()>5){
            this.connTime=connTime;
        }
    }


    public void start(){
        try {
            if (clientChannelContext!=null&&!clientChannelContext.isClosed) //已经连上了，不需要再重连了
            {
                return;
            }
            tioClient = new TioClient(clientTioConfig);
            clientChannelContext = tioClient.connect(serverNode,connTime);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void close(){
        if (clientChannelContext!=null&&!clientChannelContext.isClosed) //已经连上了，不需要再重连了
        {
            if( this.isReconnConf){
                Tio.remove(clientChannelContext,"");
            }else{
                Tio.close(clientChannelContext,"");
            }
            tioClient.stop();
        }

    }




}
